Preparation of dataset:
library(ggplot2)
library(plotly)
# Sets directory with .txt files for each subjects
mypath = "/home/finc/Dropbox/Copy/001_RESEARCH/001_PROJECTS/001_NOW/000_LearningBrain/2018/training_progress/raw"
setwd(mypath)
# Creates list of text files
txt_files_ls = list.files(path = mypath, pattern = "*.txt")
# Reads the files (columns separated with tab)
txt_files_df <- lapply(txt_files_ls, function(x) {read.table(file = x, header = F, sep ="\t")})
# Combines all text file into one data frame
train_progress <- do.call("rbind", lapply(txt_files_df, as.data.frame))
# Adds column with subjects IDs and sesion number
subjects <- substr(txt_files_ls, 1, 5) # cuts subjects' ids
id <- cbind(rep(subjects, each = 18)) # multiplies them by number of session (same for each subject) and changes row vector to a column vector
session <- cbind(rep(c(1:18), times = 19)) # creates column vector with numbers of sessions
training_progress <- cbind(id, session, train_progress) # adds IDs as a new colum
# Changes n-back level variables to numeric
for (i in 3:22){
training_progress[, i] <- as.numeric(training_progress[, i])
}
# Calculates mean of obtained n-back level during one training session session
training_progress$mean_level <- rowMeans(training_progress[, 3:22], na.rm = T)
training_progress$max_level <- do.call(pmax, training_progress[3:22])
# Summary of the data
# str(training_progress)
# summary(training_progress)
Mean n-back level for all subjects:
# Mean n-back level for all subjects, all sessions
lm(mean_level ~ session, data = training_progress)
##
## Call:
## lm(formula = mean_level ~ session, data = training_progress)
##
## Coefficients:
## (Intercept) session
## 2.22152 0.07271
# Scatter plot
g <- ggplot(training_progress,
aes(x = session, y = mean_level))
g <- g + geom_point()
g <- g + geom_smooth(method = "lm")
g <- g + theme_bw()
g <- g + ggtitle("Mean n-back level for all subjects")
g <- g + xlab("Session")
g <- g + ylab("Mean n-back level")
g <- g + theme_bw()
ggplotly()
Mean n-back level for each individual:
# Fits regression line to each individual's progress
slope_mean <- c()
intercept_mean <- c()
for (i in subjects){
sub_data = training_progress[training_progress$id == i, ]
line = lm(mean_level ~ session, data = sub_data)
intercept_mean[i] <- line$coefficients[1]
slope_mean[i] <- line$coefficients[2]
}
# Makes interactive plot with mean n-back level for each subject
g <- ggplot(training_progress,
aes(x = session, y = mean_level))
g <- g + geom_line(data = training_progress,
aes(x = session, y = mean_level, group = id),
colour = "gray") #, alpha = 1/2, size = 1/2)
g <- g + geom_smooth(method = "lm", size = 0.5)
g <- g + geom_line(size = 0.5)
g <- g + facet_wrap(~ id)
g <- g + theme_bw()
# g <- g + ggtitle("Mean n-back level for each subject")
g <- g + xlab("Session")
g <- g + ylab("Mean n-back level")
ggplotly()
# Histograms for intercept and slope
hist(intercept_mean, xlab = "Intercept")

hist(slope_mean, xlab = "Slope")

Maximum n-back level for all subjects:
# Maximum n-back level for all subjects, all sessions
lm(max_level ~ session, data = training_progress)
##
## Call:
## lm(formula = max_level ~ session, data = training_progress)
##
## Coefficients:
## (Intercept) session
## 3.0609 0.1007
# Scatter plot
g <- ggplot(training_progress,
aes(x = session, y = max_level))
g <- g + geom_point()
g <- g + geom_smooth(method = "lm")
g <- g + theme_bw()
g <- g + ggtitle("Maximum n-back level for all subjects")
g <- g + xlab("Session")
g <- g + ylab("Maximum n-back level")
g <- g + theme_bw()
ggplotly()
Maximum n-back level for each individual:
# Fits regression line to each individual's progress
slope_max <- c()
intercept_max <- c()
for (i in subjects){
sub_data = training_progress[training_progress$id == i, ]
line = lm(max_level ~ session, data = sub_data)
intercept_max[i] <- line$coefficients[1]
slope_max[i] <- line$coefficients[2]
}
# Makes interactive plot with mean n-back level for each subject
g <- ggplot(training_progress,
aes(x = session, y = max_level))
g <- g + geom_line(data = training_progress,
aes(x = session, y = max_level, group = id),
colour = "gray") #, alpha = 1/2, size = 1/2)
g <- g + geom_smooth(method = "lm", size = 0.5)
g <- g + geom_line(size = 0.5)
g <- g + facet_wrap(~ id)
g <- g + theme_bw()
# g <- g + ggtitle("Maximum n-back level for each subject")
g <- g + xlab("Session")
g <- g + ylab("Maximum n-back level")
ggplotly()
# Histograms for intercept and slope
hist(intercept_max, xlab = "Intercept")

hist(slope_max, xlab = "Slope")

Global maximum n-back level for each individual:
max_lev <- c()
for (i in subjects){
sub_data = training_progress[training_progress$id == i, ]
max_lev[i] = max(sub_data$max_level, na.rm = TRUE)
}
hist(max_lev)

Merging data in one data frame and saving it to .csv file:
progress <- data.frame(id, intercept_mean, slope_mean, intercept_max, slope_max, max_lev)
## Warning in data.frame(id, intercept_mean, slope_mean, intercept_max,
## slope_max, : row names were found from a short variable and have been
## discarded
write.csv(progress, file = "training_progress.csv", row.names = FALSE)